package mo.golden;

import mo.common.TreeNode;

public class L04_05 {

    public boolean isValidBST(TreeNode root) {
        if (root == null) {
            return true;
        }
        return validate(root.left, null, root.val) && validate(root.right, root.val, null);
    }


    private boolean validate(TreeNode node, Integer min, Integer max) {
        if (node == null) {
            return true;
        }
        if (min != null && node.val <= min) {
            return false;
        }
        if (max != null && node.val >= max) {
            return false;
        }
        return validate(node.left, min, node.val) && validate(node.right, node.val, max);
    }


}
